//
//  SAReturnGoodsVC.m
//  saasRetail
//
//  Created by 黄志刚 on 2021/7/19.
//

#import "SAReturnGoodsVC.h"
#import "SAReturnDocumentsVC.h"
#import "SAReturnGoodsListVC.h"
#import "SAReturnGoodsCell.h"
#import "SASalesInvoiceDetailModel.h"
#import "SASearchStoreUserView.h"
#import "UILabel+NSMutableAttributedString.h"
#import "SAPayView.h"
#import "SASalesReturnSaveAPI.h"
#import "SASalesReturnDetailAPI.h"
#import "SAPrintReturn.h"
#import "SACloudPrintPushPrintMsgAPI.h"
#import "NSString+nonce.h"
#import "NSString+MD5.h"
#import "SAQueryPrinterStatusAPI.h"
#import "SAPrintTicketAPI.h"

@interface SAReturnGoodsVC ()<UITableViewDataSource, UITableViewDelegate, HTTPAPIDelegate>

@property (nonatomic, strong) UIButton *empBtn;
@property (nonatomic, strong) UIButton *dateBtn;
@property (nonatomic, strong) UITextField *dateTf;
@property (nonatomic, strong) UILabel *shopLab;
@property (nonatomic, strong) UILabel *shopPirceLab;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, assign) float tableViewTop;
@property (nonatomic, strong) UIButton *addBtn;
@property (nonatomic, strong) NSMutableArray<SASaleSkuListModel *> *dataMarr;
@property (nonatomic, assign) NSInteger arrCount;
@property (nonatomic, strong) UIView *footView;
@property (nonatomic, strong) UILabel *returnCountLab;
@property (nonatomic, strong) SASalesInvoiceDetailModel *salesInvoiceDetailModel;
@property (nonatomic, strong) SAStoreUserModel *userModel;
@property (nonatomic, strong) NSString *payType;
@property (nonatomic, strong) SASalesReturnSaveAPI *salesReturnSaveAPI;
@property (nonatomic, strong) SASalesReturnDetailAPI *salesReturnDetailAPI;
@property (nonatomic, strong) SAOverallSetting *overallSetting;
@property (nonatomic, strong) SACloudPrintPushPrintMsgAPI *cloudPrintPushPrintMsgAPI;
@property (nonatomic, strong) UIButton *settlementBtn;
@property (nonatomic, strong) NSString *randomStr;

@property (nonatomic, strong) SAQueryPrinterStatusAPI *queryPrinterStatusAPI;
@property (nonatomic, strong) SAPrintTicketAPI *printTicketAPI;
@property (nonatomic, strong) SASalesInvoiceDetailModel *printModel;

@end

@implementation SAReturnGoodsVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.overallSetting = [SAOverallSetting shareInstance];
}

#pragma mark - HTTPAPIDelegate
- (void)fetchData:(id)data fromServerSuccessedWithAPI:(HTTPAPI *)api {
    if (self.salesReturnSaveAPI == api) {
        [Prompt popPromptViewWithMsg:@"退款成功" duration:2];
        [self.dataMarr removeAllObjects];
        [self.tableView reloadData];
        [self setUI];
        NSString *salesOrderId = data[@"data"][@"salesOrderId"];
        self.salesReturnDetailAPI.salesReturnOrderId = salesOrderId;
        [self.salesReturnDetailAPI sendRequestToServerWithActivityViewAndMask:GET];
        
    }else if (self.salesReturnDetailAPI == api) {
        SASalesInvoiceDetailModel *salesInvoiceDetailModel = [SASalesInvoiceDetailModel mj_objectWithKeyValues:data[@"data"]];
        self.printModel = salesInvoiceDetailModel;
        if (self.overallSetting.printerSetting.printerStatus == 2 && [SAValueManger shareInStance].printType == 1) {
            [SAPrintReturn printWithTotalData:salesInvoiceDetailModel printSuccessBlock:^{
                
            }];
        }else {
            [self.queryPrinterStatusAPI submitForm:POST];
        }
    }else if (self.cloudPrintPushPrintMsgAPI == api) {
        [Prompt popPromptViewWithMsg:@"正在进行共享打印" duration:2];
    }else if (self.queryPrinterStatusAPI == api) {
        NSString *status = data[@"data"][@"status"];
        NSString *type = data[@"data"][@"type"];
        if ([status isEqualToString:@"1"] && [type isEqualToString:@"1"]) {
            self.printTicketAPI.ticketType = @"2";
            self.printTicketAPI.salesOrderId = self.printModel.saleOrderId;
            [self.printTicketAPI submitForm:POST];
        }else {
            if (kIsTestServer == 1) {
                self.cloudPrintPushPrintMsgAPI.merchantId = [SAValueManger shareInStance].userInfo.merchantId;
                self.cloudPrintPushPrintMsgAPI.type = @"2";
                self.cloudPrintPushPrintMsgAPI.data = self.printModel.saleOrderId;
                [self.cloudPrintPushPrintMsgAPI submitForm:POST];
            }else {
                [Prompt popPromptViewWithMsg:@"未连接打印机" duration:2];
            }
        }
    }
}

- (void)fetchDataFromServerFailedWithError:(NSError *)error API:(HTTPAPI *)api {
    if (self.salesReturnSaveAPI == api) {
        NSDictionary *dict = error.userInfo;
        if ([[NSString stringWithFormat:@"%@",dict[@"code"]] isEqualToString:@"50001"]) {
            NSString *msg = dict[@"msg"];
            [Prompt popPromptViewWithMsg:msg duration:3];
            [self.dataMarr removeAllObjects];
            [self.tableView reloadData];
            [self setUI];
        }else if ([[NSString stringWithFormat:@"%@",dict[@"code"]] isEqualToString:@"50002"]) {
            NSString *msg = dict[@"msg"];
            [Prompt popPromptViewWithMsg:msg duration:3];
            [self.dataMarr removeAllObjects];
            [self.tableView reloadData];
            [self setUI];
        }
    }else if (self.queryPrinterStatusAPI == api) {
        if (kIsTestServer == 1) {
            self.cloudPrintPushPrintMsgAPI.merchantId = [SAValueManger shareInStance].userInfo.merchantId;
            self.cloudPrintPushPrintMsgAPI.type = @"2";
            self.cloudPrintPushPrintMsgAPI.data = self.printModel.saleOrderId;
            [self.cloudPrintPushPrintMsgAPI submitForm:POST];
        }else {
            [Prompt popPromptViewWithMsg:@"未连接打印机" duration:2];
        }
    }
}

#pragma mark - UITableView Delegate & DataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataMarr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SAReturnGoodsCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SAReturnGoodsCell" forIndexPath:indexPath];
    cell.model = self.dataMarr[indexPath.row];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return kScaleNum(55);
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return [[UIView alloc] initWithFrame:CGRectZero];
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [[UIView alloc] initWithFrame:CGRectZero];
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return kScaleNum(0.01);
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 10;
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}

-(NSArray<UITableViewRowAction*>*)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
    WeakSelf
    SASaleSkuListModel *model = self.dataMarr[indexPath.row];
    UITableViewRowAction *rowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault
                                                                         title:@"删除" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
        [weakSelf.dataMarr removeObject:model];
        [weakSelf setUI];
        [tableView reloadData];
    }];
    rowAction.backgroundColor = kPinkColor;
    NSArray *arr = @[rowAction];
    return arr;
}

- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{
    return @"删除";
}

#pragma mark - event
-(void)clickedListBtn {
    NSMutableArray *menuList = [SAValueManger shareInStance].userInfo.menuList;
    NSString *isOrder = [NSString stringWithFormat:@"%@",menuList[4][@"isDelete"]];
    NSString *orderReturnList = [NSString stringWithFormat:@"%@",menuList[4][@"perms"][@"orderReturnList"]];
    
    if ([isOrder isEqualToString:@"1"]) {
        [Prompt popPromptViewWithMsg:@"您没有订单权限,请联系店长添加" duration:2];
        return;
    }else if ([orderReturnList isEqualToString:@"1"]) {
        [Prompt popPromptViewWithMsg:@"您没有退货列表权限,请联系店长添加" duration:2];
        return;
    }
    
    SAReturnDocumentsVC *pushVC = [SAReturnDocumentsVC new];
    [self.navigationController pushViewController:pushVC animated:YES];
}

-(void)clickedUserBtn {
    if (self.userModel == nil) {
        [Prompt popPromptViewWithMsg:@"请先添加退货订单" duration:2];
        return;
    }
    SASearchStoreUserView *searchStoreUserView = [SASearchStoreUserView shareCornView];
    UIViewController *vc = nil;
    [searchStoreUserView showView:vc];
    WeakSelf
    searchStoreUserView.selectBlock = ^(SAStoreUserModel * _Nonnull userModel) {
        weakSelf.userModel = userModel;
        if (userModel.mobile.length > 0) {
            [weakSelf.empBtn setTitle:[NSString stringWithFormat:@"%@(%@)",userModel.empName,userModel.mobile] forState:UIControlStateNormal];
        }else {
            [weakSelf.empBtn setTitle:[NSString stringWithFormat:@"%@",userModel.empName] forState:UIControlStateNormal];
        }
        CGFloat imageWidth = weakSelf.empBtn.imageView.bounds.size.width;
        CGFloat labelWidth = weakSelf.empBtn.titleLabel.bounds.size.width;
        weakSelf.empBtn.imageEdgeInsets = UIEdgeInsetsMake(0, labelWidth, 0, -labelWidth);
        weakSelf.empBtn.titleEdgeInsets = UIEdgeInsetsMake(0, -imageWidth-10, 0, imageWidth + 10);
        [weakSelf.empBtn setTitleColor:kBlackColor forState:UIControlStateNormal];
    };
}

-(void)clickedDateBtn {
    
}

-(void)clickedAddBtn {
    SAReturnGoodsListVC *pushVC = [SAReturnGoodsListVC new];
    WeakSelf
    pushVC.goodsBlock = ^(NSMutableArray * _Nonnull marr, SASalesInvoiceDetailModel * _Nonnull model) {
        weakSelf.dataMarr = marr;
        weakSelf.arrCount = marr.count;
        weakSelf.salesInvoiceDetailModel = model;
        [weakSelf.tableView reloadData];
        [weakSelf popVC:@"SAReturnGoodsVC" :YES];
        SAStoreUserModel *userModel = [SAStoreUserModel new];
        userModel.empId = model.empId;
        userModel.empName = model.empName;
        userModel.empType = model.empType;
        userModel.mobile = model.empMobile;
        weakSelf.userModel = userModel;
        
        weakSelf.dateTf.text = self.salesInvoiceDetailModel.memberName;
        
        if (userModel.mobile.length > 0) {
            [weakSelf.empBtn setTitle:[NSString stringWithFormat:@"%@(%@)",userModel.empName,userModel.mobile] forState:UIControlStateNormal];
        }else {
            [weakSelf.empBtn setTitle:[NSString stringWithFormat:@"%@",userModel.empName] forState:UIControlStateNormal];
        }
        CGFloat imageWidth = weakSelf.empBtn.imageView.bounds.size.width;
        CGFloat labelWidth = weakSelf.empBtn.titleLabel.bounds.size.width;
        weakSelf.empBtn.imageEdgeInsets = UIEdgeInsetsMake(0, labelWidth, 0, -labelWidth);
        weakSelf.empBtn.titleEdgeInsets = UIEdgeInsetsMake(0, -imageWidth-10, 0, imageWidth + 10);
        [weakSelf.empBtn setTitleColor:kBlackColor forState:UIControlStateNormal];
        [weakSelf setUI];
    };
    [self.navigationController pushViewController:pushVC animated:YES];
}

//防止多次结单
- (void)unisversalButtonAcrion:(UIButton *)sender
{
    sender.userInteractionEnabled = NO;
    [[self class] cancelPreviousPerformRequestsWithTarget:self selector:@selector(clickedSettlementBtn) object:sender];
    [self performSelector:@selector(clickedSettlementBtn) withObject:sender afterDelay:0.2f];
}

-(void)clickedSettlementBtn {
    WeakSelf
    BOOL isVip = NO;
    if (self.salesInvoiceDetailModel.memberName.length > 0 && ![self.salesInvoiceDetailModel.memberName isEqualToString:@"散客"]) {
        isVip = YES;
    }
    SAPayView *payView = [[SAPayView alloc] initWithTotalPrice:self.returnCountLab.text AndIsVip:isVip IsRuturn:YES];
    [payView showView:self];
    self.settlementBtn.userInteractionEnabled = YES;
    
    payView.settlementBlock = ^(NSString *payType) {
        weakSelf.payType = payType;
        [weakSelf Settlement];
    };
}

-(void)Settlement {
    self.salesReturnSaveAPI.coverParameters = [self settlementData];
    [self.salesReturnSaveAPI sendRequestToServerWithActivityViewAndMask:POST];
}

//结算上传的数据
-(NSMutableDictionary *)settlementData {
    
    NSString *actualPrice = [NSString stringWithFormat:@"%0.lf",[self.returnCountLab.text doubleValue] * 100];
    NSMutableDictionary *mdic = [NSMutableDictionary dictionary];
    NSMutableArray *payAddFormArray = [[NSMutableArray alloc] init];
    NSMutableArray *detailFormArray = [[NSMutableArray alloc] init];
    
    mdic[@"empId"] = self.userModel.empId;
    mdic[@"orderNo"] = self.salesInvoiceDetailModel.saleOrderId;
    mdic[@"actualPrice"] = actualPrice;
    mdic[@"empType"] = self.userModel.empType;
    mdic[@"shouldPrice"] = actualPrice;
    mdic[@"deviceId"] = [NSString stringWithFormat:@"%@-%@", kDeviceId, self.userModel.empId];
    NSMutableDictionary *payAddFormDic = [[NSMutableDictionary alloc] init];
    if ([self.payType integerValue] > 2) {
        payAddFormDic[@"payType"] = [NSString stringWithFormat:@"%zd",[self.payType integerValue] + 1];
    }else {
        payAddFormDic[@"payType"] = self.payType;
    }
    payAddFormDic[@"payPrice"] = actualPrice;
    [payAddFormArray addObject:payAddFormDic];
    mdic[@"payForms"] = payAddFormArray;
    
    for (SASaleSkuListModel *model in self.dataMarr) {
        if ([model.stock integerValue] == 0) {
            continue;
        }
        NSMutableDictionary *detailFormDic = [[NSMutableDictionary alloc] init];
        detailFormDic[@"count"] = model.stock;
        detailFormDic[@"remark"] = @"";
        detailFormDic[@"skuId"] = model.skuId;
        detailFormDic[@"giveFlag"] = model.giveFlag;
        [detailFormArray addObject:detailFormDic];
    }
    mdic[@"detailForms"] = detailFormArray;
    NSData *nData = [NSJSONSerialization dataWithJSONObject:mdic options:0 error:nil];
    mdic[@"randomStr"] = [NSString stringWithFormat:@"%@%@",self.randomStr,[NSString getMD5WithData:nData]];
    
    return mdic;
}

-(void)setUI {
    if (self.dataMarr.count == 0) {
        self.userModel = nil;
        [self.empBtn setTitle:@" " forState:UIControlStateNormal];
        [self.empBtn setImage:[UIImage imageNamed:@"Sale_rightArrow"] forState:UIControlStateNormal];
        CGFloat imageWidth = self.empBtn.imageView.bounds.size.width;
        CGFloat labelWidth = self.empBtn.titleLabel.bounds.size.width;
        self.empBtn.imageEdgeInsets = UIEdgeInsetsMake(0, labelWidth, 0, -labelWidth);
        self.empBtn.titleEdgeInsets = UIEdgeInsetsMake(0, -imageWidth-10, 0, imageWidth + 10);
        [self.empBtn setTitleColor:kBlackColor forState:UIControlStateNormal];
        self.dateTf.text = @"";
        self.footView.hidden = YES;
        self.shopLab.text = @"选择订单";
        self.shopPirceLab.text = @"";
        self.returnCountLab.text = @"";
        self.randomStr = [NSString random:10];
    }else {
        self.footView.hidden = NO;
        double total = 0.0;
        NSInteger count = 0;
        for (SASaleSkuListModel *model in self.dataMarr) {
            total += [model.skuActualPrice doubleValue]/100 * [model.stock integerValue];
            count += [model.stock integerValue];
        }
        self.shopLab.text = [NSString stringWithFormat:@"选择订单(%zd)",count];
        [self.shopLab setAttributedStringWithSubString:[NSString stringWithFormat:@"%zd",count] color:kPinkColor];
        
        if (self.salesInvoiceDetailModel.isAllReturn == YES && self.arrCount == self.dataMarr.count) {
            total = [self.salesInvoiceDetailModel.actualPrice doubleValue]/100 - self.salesInvoiceDetailModel.returnTotal;
        }
        self.shopPirceLab.text = [NSString stringWithFormat:@"￥%0.2lf",total];
        self.returnCountLab.text = [NSString stringWithFormat:@"%0.2lf",total];
    }
}

#pragma mark - initUI
-(void)initUI {
    [self setNavigationItemBackBBIAndTitle:@"退货"];
    [self setBgImage:nil];
    self.randomStr = [NSString random:10];
    self.view.backgroundColor = kBgColor;
    
    UIButton *listBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    listBtn.frame = CGRectMake(0, 0, kScaleNum(60), kScaleNum(44));
    [listBtn setImage:[UIImage imageNamed:@"shopSale_list"] forState:UIControlStateNormal];
    [listBtn addTarget:self action:@selector(clickedListBtn) forControlEvents:UIControlEventTouchUpInside];
    [self addRightViews:@[listBtn]];
    
    UIView *whiteView = [[UIView alloc] initWithFrame:SARectMake(0, StatusHeight, kBaseScreenWidth, 75)];
    whiteView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:whiteView];
    
    NSArray *nameArr = @[@"  店员", @"  会员"];
    NSArray *imageArr = @[@"Sale_user", @"Sale_vip"];
    for (int i = 0; i < nameArr.count; i++) {
        UIButton *titleBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [titleBtn setImage:[UIImage imageNamed:imageArr[i]] forState:UIControlStateNormal];
        [titleBtn setTitle:nameArr[i] forState:UIControlStateNormal];
        titleBtn.frame = SARectMake(20, StatusHeight + i * 38, 120, 37);
        titleBtn.titleLabel.font = kFont(15);
        [titleBtn setTitleColor:kBlackColor forState:UIControlStateNormal];
        titleBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        [self.view addSubview:titleBtn];
        
        if (i == 0) {
            UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
            [rightBtn setImage:[UIImage imageNamed:@"Sale_rightArrow"] forState:UIControlStateNormal];
            rightBtn.frame = SARectMake(0, StatusHeight + i * 38, kBaseScreenWidth - 20, 37);
            rightBtn.titleLabel.font = kFont(15);
            [rightBtn setTitleColor:kBlackColor forState:UIControlStateNormal];
            rightBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
            [self.view addSubview:rightBtn];
            
            CGFloat imageWidth = rightBtn.imageView.bounds.size.width;
            CGFloat labelWidth = rightBtn.titleLabel.bounds.size.width;
            rightBtn.imageEdgeInsets = UIEdgeInsetsMake(0, labelWidth, 0, -labelWidth);
            rightBtn.titleEdgeInsets = UIEdgeInsetsMake(0, -imageWidth-10, 0, imageWidth + 10);
            
            [rightBtn addTarget:self action:@selector(clickedUserBtn) forControlEvents:UIControlEventTouchUpInside];
            self.empBtn = rightBtn;
        }else {
            UITextField *dateTf = [[UITextField alloc] initWithFrame:SARectMake(0, 7, 249, 30)];
            dateTf.placeholder = @"";
            dateTf.text = @"";
            dateTf.userInteractionEnabled = NO;
            dateTf.font = kFont(15);
            dateTf.textColor = kBlackColor;
            dateTf.frame = SARectMake(kBaseScreenWidth - 270, StatusHeight + i * 38, 250, 37);
            dateTf.textAlignment = NSTextAlignmentRight;
            self.dateTf = dateTf;
            [self.view addSubview:dateTf];
        }
        
        if (i == 0) {
            UIView *line = [[UIView alloc] initWithFrame:SARectMake(20, StatusHeight + i * 38 + 37, kBaseScreenWidth - 40, 1)];
            line.backgroundColor = kLineColor;
            [self.view addSubview:line];
        }
    }
    
    UIView *whiteView2 = [[UIView alloc] initWithFrame:CGRectMake(0, whiteView.maxY + kScaleNum(7), kScreenWidth, kScaleNum(37))];
    whiteView2.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:whiteView2];
    
    UILabel *shopLab = [[UILabel alloc] initWithFrame:SARectMake(20, 0, 150, 37)];
    shopLab.text = @"选择订单";
    shopLab.textColor = kBlackColor;
    shopLab.font = kFont(15);
    [whiteView2 addSubview:shopLab];
    self.shopLab = shopLab;
    
    UILabel *shopPirceLab = [[UILabel alloc] initWithFrame:SARectMake(kBaseScreenWidth - 190, 0, 170, 37)];
    shopPirceLab.textColor = kBlackColor;
    shopPirceLab.text = @"";
    shopPirceLab.font = kFont(15);
    shopPirceLab.textAlignment = NSTextAlignmentRight;
    [whiteView2 addSubview:shopPirceLab];
    self.shopPirceLab = shopPirceLab;
    
    self.tableViewTop = whiteView2.maxY;
    [self.view addSubview:self.tableView];
    
    UIButton *addBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [addBtn setImage:[UIImage imageNamed:@"Sale_Add"] forState:UIControlStateNormal];
    addBtn.frame = CGRectMake(kScreenWidth - kScaleNum(70), kScreenHeight - kScaleNum(200), kScaleNum(53), kScaleNum(52));
    addBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [addBtn addTarget:self action:@selector(clickedAddBtn) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:addBtn];
    self.addBtn = addBtn;
    
    UIView *footView = [[UIView alloc] initWithFrame:CGRectMake(0, kScreenHeight - kScaleNum(75) - SafeFooterHeight, kScreenWidth, kScaleNum(75) + SafeFooterHeight)];
    footView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:footView];
    footView.hidden = YES;
    self.footView = footView;
    
    UILabel *returnLab = [[UILabel alloc] initWithFrame:SARectMake(20, 0, 100, 35)];
    returnLab.text = @"实际退款";
    returnLab.font = kFont(15);
    returnLab.textColor = kBlackColor;
    [footView addSubview:returnLab];
    
    UILabel *returnCountLab = [[UILabel alloc] initWithFrame:SARectMake(kBaseScreenWidth - 210, 0, 190, 35)];
    returnCountLab.font = kFont(15);
    returnCountLab.textColor = kBlackColor;
    returnCountLab.textAlignment = NSTextAlignmentRight;
    [footView addSubview:returnCountLab];
    self.returnCountLab = returnCountLab;
    
    UIButton *settlementBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    settlementBtn.frame = CGRectMake(0, kScaleNum(35), kScreenWidth, kScaleNum(40) + SafeFooterHeight);
    [settlementBtn setBackgroundImage:[UIImage imageNamed:@"top_image"] forState:UIControlStateNormal];
    [settlementBtn setTitle:@"结算" forState:UIControlStateNormal];
    settlementBtn.titleLabel.font = kBFont(18);
    [settlementBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [settlementBtn addTarget:self action:@selector(unisversalButtonAcrion:) forControlEvents:UIControlEventTouchUpInside];
    [footView addSubview:settlementBtn];
    self.settlementBtn = settlementBtn;
}

#pragma mark - lazy
-(UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, self.tableViewTop, kScreenWidth, kScreenHeight - self.tableViewTop - kScaleNum(80) - SafeFooterHeight) style:UITableViewStylePlain];
        if (@available(iOS 15.0, *)) {
            _tableView.sectionHeaderTopPadding = 0;
        }
        _tableView.backgroundColor = kBgColor;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.showsHorizontalScrollIndicator = NO;
        if (@available(iOS 11.0, *)) {
            _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }
        _tableView.estimatedRowHeight = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
        _tableView.estimatedSectionFooterHeight = 0;
        [_tableView registerClass:[SAReturnGoodsCell class] forCellReuseIdentifier:@"SAReturnGoodsCell"];
    }
    return _tableView;
}

-(NSMutableArray<SASaleSkuListModel *> *)dataMarr {
    if (!_dataMarr) {
        _dataMarr = [NSMutableArray array];
    }
    return _dataMarr;
}

-(SASalesReturnSaveAPI *)salesReturnSaveAPI {
    if (!_salesReturnSaveAPI) {
        _salesReturnSaveAPI = [SASalesReturnSaveAPI apiWithDelegate:self];
    }
    return _salesReturnSaveAPI;
}

-(SASalesReturnDetailAPI *)salesReturnDetailAPI {
    if (!_salesReturnDetailAPI) {
        _salesReturnDetailAPI = [SASalesReturnDetailAPI apiWithDelegate:self];
    }
    return _salesReturnDetailAPI;
}

-(SACloudPrintPushPrintMsgAPI *)cloudPrintPushPrintMsgAPI {
    if (!_cloudPrintPushPrintMsgAPI) {
        _cloudPrintPushPrintMsgAPI = [SACloudPrintPushPrintMsgAPI apiWithDelegate:self];
    }
    return _cloudPrintPushPrintMsgAPI;
}

-(SAQueryPrinterStatusAPI *)queryPrinterStatusAPI {
    if (!_queryPrinterStatusAPI) {
        _queryPrinterStatusAPI = [SAQueryPrinterStatusAPI apiWithDelegate:self];
    }
    return _queryPrinterStatusAPI;
}

-(SAPrintTicketAPI *)printTicketAPI {
    if (!_printTicketAPI) {
        _printTicketAPI = [SAPrintTicketAPI apiWithDelegate:self];
    }
    return _printTicketAPI;
}

@end
